/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CDialogoGradeBdFormBd.java
 *
 * Created on 25/05/2010, 09:34:51
 */
package rpt.bd.cenario;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

/**
 *
 * @author Roberto
 */
public class CDialogoGradeBdFormBd extends javax.swing.JPanel {

    private CCenarioBdEdicao fCenarioBdEdicao;
    //
    private boolean fPreparar = true;
    private boolean fPreparado = false;
    //
    private CPainelBdCenarioEdicao fPainelBd = null;
    //1//private CGradeBdCenarioEdicao fGradeBd = null;
    private CFiltroGradeBdCenarioEdicao fGradeBd = null;
    private CModeloGradeBdCenario fModeloGradeBd = null;
    private CPersistenciaBd fPertinenciaBd = null;
    private CConjuntoDadosBd fConjuntoDadosBd = null;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;

    /** Creates new form CDialogoGradeBdFormBd */
    public CDialogoGradeBdFormBd(CCenarioBdEdicao aCenarioBd) {
        //
        super();
        //
        initComponents();
        //
        fCenarioBdEdicao = aCenarioBd;
        //
        addComponentListener(new ComponentListener() {

            @Override
            public void componentHidden(ComponentEvent e) {
                System.err.println(e.getComponent().getClass().getName() + " --- Hidden");
            }

            @Override
            public void componentMoved(ComponentEvent e) {
                System.err.println(e.getComponent().getClass().getName() + " --- Moved");
            }

            @Override
            public void componentResized(ComponentEvent e) {
                System.err.println(e.getComponent().getClass().getName() + " --- Resized ");
            }

            @Override
            public void componentShown(ComponentEvent e) {
                System.err.println(e.getComponent().getClass().getName() + " --- Shown");

            }
        });
        //
        addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                System.err.println(e.getComponent().getClass().getName() + " --- focusGained");
            }

            @Override
            public void focusLost(FocusEvent e) {
                System.err.println(e.getComponent().getClass().getName() + " --- focusLost");
            }
        });
        if (fPreparar) {
            preparar();
        }
        //1//fGradeBd = new CGradeBdCenarioEdicao(aCenarioBd);
    }
    
    public void liberarPreparacao() {
        fPreparar = true;
    }

    public void preparar() {

        if ((fPreparar) && (!fPreparado)) {
            fPreparado = true;
            fPertinenciaBd = fCenarioBdEdicao.getPertinenciaBd();
            fConjuntoDadosBd = fCenarioBdEdicao.getConjuntoDadosBd();
            fPainelBd = (CPainelBdCenarioEdicao) fCenarioBdEdicao.getPainelBd();
            fGradeBd = new CFiltroGradeBdCenarioEdicao(fCenarioBdEdicao);
            fModeloGradeBd = (CModeloGradeBdCenario) fGradeBd.getModel();

            if (fGradeBd.getRolar()) {
                jScrollPane1 = new javax.swing.JScrollPane();
                jScrollPane1.setViewportView(fGradeBd);
                jSplitPane1.setLeftComponent(jScrollPane1);
            } else {
                jSplitPane1.setLeftComponent(fGradeBd);
            }
            if (fPainelBd.getRolar()) {
                jScrollPane2 = new javax.swing.JScrollPane();
                jScrollPane2.setViewportView(fPainelBd);
                jSplitPane1.setRightComponent(jScrollPane2);
            } else {
                jSplitPane1.setRightComponent(fPainelBd);
            }
        }
    }

    /** Creates new form CDialogoGradeBdFormBd * /
    public CDialogoGradeBdFormBd(
    final CCenarioBdEdicao aCenarioBd,
    final JComponent aComponenteEsquerdaa,
    final JComponent aComponenteDireitaa) {
    super();
    initComponents();
    //
    fCenarioBdEdicao = aCenarioBd;
    //
    preparar1(); 
    //
    jScrollPane1 = new javax.swing.JScrollPane();
    jScrollPane2 = new javax.swing.JScrollPane();
    
    jScrollPane1.setViewportView(fGradeBd);
    jScrollPane2.setViewportView(fPainelBd);
    
    jSplitPane1.setLeftComponent(jScrollPane1);
    jSplitPane1.setRightComponent(jScrollPane2);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();

        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(400);
        jSplitPane1.setAutoscrolls(true);
        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    public void incluir() {
        int vIndiceNovoRegistroBd = fConjuntoDadosBd.getConjuntoDados().size();
        final CRegistroBd r = new CRegistroBd(fCenarioBdEdicao.getPertinenciaBd(), null);
        fConjuntoDadosBd.eventoDepoisInserirRegistro(r);
        fModeloGradeBd.addRow(r);
        fGradeBd.getSelectionModel().setSelectionInterval(vIndiceNovoRegistroBd, vIndiceNovoRegistroBd);
        //fGradeBd.setRegistroBdAtual(vIndiceNovoRegistroBd);
        r.atualizarPainelBd(true);
    }

    public void alterar() {
        if (fGradeBd != null) {
            final CRegistroBd r = (CRegistroBd) fGradeBd.getRegistroBdAtual();
            if (r != null) {
                r.atualizarPainelBd(true);
            }
        }
    }

    public void salvar() {
        CRegistroBd r = fGradeBd.getRegistroBdAtual();
        if (r == null) {
            fPainelBd.limparComponentes();
        } else {
            r.atualizarPainelBd(false);
        }
    }

    public void cancelar() {
        CRegistroBd r = fGradeBd.getRegistroBdAtual();
        if (r == null) {
            fPainelBd.limparComponentes();
        } else {
            r.atualizarPainelBd(false);
        }
    }

    public void excluir() {
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration//GEN-END:variables
}
